package sz.com.postoffice.jxyzwgcase.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.dao.BaseDao;
import sz.com.postoffice.jxyzwgcase.dao.master.MonthPlanDao;
import sz.com.postoffice.jxyzwgcase.dao.master.PersonnelSectionDao;
import sz.com.postoffice.jxyzwgcase.pojo.MonthPlan;
import sz.com.postoffice.jxyzwgcase.pojo.PersonnelSectionInformation;
import sz.com.postoffice.jxyzwgcase.service.MonthPlanService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MonthPlanServcieImpl
 * @Deacription 月度计划逻辑处理层
 * @Author jmx
 * @Date 2020/11/12 18:00
 * @Version 1.0
 **/
@Service
@Slf4j
public class MonthPlanServcieImpl extends BaseServiceImpl<MonthPlan> implements MonthPlanService {
    @Autowired
    private PersonnelSectionDao personnelSectionDao;
    @Resource
    private MonthPlanDao monthPlanDao;
    @Override
    protected BaseDao<MonthPlan> getMapper() {
        return this.monthPlanDao;
    }

    @Override
    public TableDataInfo findByDeptAndMonth(String deptCode, String planMonth) {
        PersonnelSectionInformation information = new PersonnelSectionInformation();
        information.setSalesNo(deptCode);
        List<PersonnelSectionInformation> personList =  personnelSectionDao.findByListEntity(information);
        List<MonthPlan> monthPlanList = new ArrayList<>();
        for(PersonnelSectionInformation person : personList){
            MonthPlan monthPlan = new MonthPlan();
            monthPlan.setUserCode(person.getUserCode());
            monthPlan.setDeptCode(deptCode);
            monthPlan.setPlanMonth(planMonth);
            MonthPlan plan = monthPlanDao.findByEntity(monthPlan);
            monthPlan.setUserName(person.getUserName());
            if (plan != null)  {
                monthPlan.setPlanAmount(plan.getPlanAmount());
                monthPlan.setId(plan.getId());
            } else {
                monthPlan.setPlanAmount(0.0);
                monthPlan.setId(0);
            }
            monthPlanList.add(monthPlan);
        }
        TableDataInfo tableDataInfo = new TableDataInfo(monthPlanList, monthPlanList.size());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("请求成功");
        return tableDataInfo;
    }

    @Override
    public boolean batchUpdate(List<MonthPlan> monthPlanList) {
        for (MonthPlan monthPlan : monthPlanList) {
            try {
                if (monthPlan.getId() > 0) {
                    monthPlanDao.update(monthPlan);
                } else {
                    monthPlanDao.insert(monthPlan);
                }
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }
}
